package databasetest;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SelectG extends JInternalFrame {
	DefaultTableModel model;
	TableRowSorter<TableModel> tSoter;
	ResultSet rs = null;
	ResultSetMetaData rsmd = null;
	PreparedStatement pstmt = null;
	Vector<String> v = null;

	UpdateG u;
	InputG i;

	Connection conn;
	GuestBookMain gbm;

	private JPanel panel;
	private JLabel lblNewLabel;
	private JTextField findStr;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	JTable table;

	public SelectG(Connection conn, GuestBookMain gbm) {
		super("방명록 조회",true,true,true,false);
		this.conn=conn;
		this.gbm=gbm;
		setBounds(100, 100, 327, 276);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}
	
	public void setD(UpdateG u) {
		this.u = u;
	}
	public void setI(InputG i) {
		this.i = i;
	}
	public void removeall(){
		for(int i=model.getRowCount()-1;i>=0;i--){
			model.removeRow(i);
		}
		model = null;
		tSoter = null;
		rs = null;
		rsmd = null;
		pstmt = null;
		v = null;
	}

	public void columnadd() {
		if (table!=null) table=null;
		String sql = "";
		
		String find = findStr.getText();
		

		

		try {    
			if(find.equals("")){
				sql = "select * from guestbook";
				pstmt = conn.prepareStatement(sql);
			}else{
				sql = "select * from guestbook where mid=? or subject like ? or content like ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,find);
				pstmt.setString(2,"%"+find+"%");
				pstmt.setString(3,"%"+find+"%");
			}


			rs = pstmt.executeQuery();// 리절트셋에 대한 부가정보를 갖는 객체얻어오기
			rsmd = rs.getMetaData();// 컬럼명 테이블에 셋팅

			int colCount = rsmd.getColumnCount();
			String t[] = new String[colCount + 1];
			t[0] = "NO";

			for (int i = 1; i <= colCount; i++) {
				String colName = rsmd.getColumnName(i);
				t[i] = colName;
			}

			for(int i=0; i<=t.length-1; i++){
				System.out.println(t[i]);
			}

			model = new DefaultTableModel(t,0);
			scrollPane.setViewportView(getTable());

			int k=0; 
			while (rs.next()) {
				v= new Vector<String>();
				k=k+1;
				v.add(""+k);
				for (int j = 1; j <= colCount; j++) {
					v.add(rs.getString(j));
				}
				model.addRow(v);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}


	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLblNewLabel(), BorderLayout.WEST);
			panel.add(getFindStr(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("찾는 자료");
		}
		return lblNewLabel;
	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					int key = e.getKeyCode();
					if(key == e.VK_ENTER){
						columnadd();
					}
				}
			});
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					columnadd();
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if(gbm.updateG == null){
						gbm.openupdate();
					}else{
						gbm.updateG.setVisible(true);
					}

					
					int a=table.getSelectedRow();
					String s = (String)table.getValueAt(a, 1);
					String c = (String)table.getValueAt(a, 2);
					String e = (String)table.getValueAt(a, 3);
					String k = (String)table.getValueAt(a, 4);
					String l = (String)table.getValueAt(a, 5);
					
					gbm.updateG.setData(s,c,e,k,l);
					
					
				}
			});
			tSoter = new TableRowSorter(model);
			table.setRowSorter(tSoter);
		}
		return table;
	}
}
